package mathive.client.panels;

import mathive.client.PanelHandler;
import mathive.client.UserService;
import mathive.client.UserServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ForgotPassPanel {
	private static final UserServiceAsync SERVICE = GWT.create(UserService.class);

	private VerticalPanel mainPanel = new VerticalPanel();
	private TextBox email = new TextBox();
	private Label email_label = new Label("E-mail");
	private Button resetButton = new Button("Reset Password");
	
	public ForgotPassPanel(){
		
		// Listen for mouse events on the register button.
		resetButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				SERVICE.resetPassword(
						email.getText(),
						new ResetResponse());
			}
		});
		
		initialize();
	}
	
	public Widget getPanel(){
		mainPanel.clear();
		initialize();
		return mainPanel;
	}
	
	private void initialize(){		
		
		mainPanel.add(new HTML("<h1>Forgot password</h1>"));
		mainPanel.add(email_label);
		mainPanel.add(email);
		mainPanel.add(resetButton);
		mainPanel.setCellHorizontalAlignment(resetButton, HasHorizontalAlignment.ALIGN_RIGHT);
		
	}
	
	private class ResetResponse implements AsyncCallback<Boolean> {
		@Override
		public void onFailure(Throwable caught) {
			mainPanel.add(new Label("Mail not found"));
		}

		@Override
		public void onSuccess(Boolean result) {
			if (result.booleanValue()) {
				PanelHandler.showHome();
			} else {
				mainPanel.add(new Label("Mail not found"));
			}
		}
	}
}
